package com.liangtian.mdm.system.button.service.impl;

import com.liangtian.mdm.system.button.dao.SysButtonDao;
import com.liangtian.mdm.system.button.model.SysButton;
import com.liangtian.mdm.system.button.model.SysPermissionButton;
import com.liangtian.mdm.system.button.model.SysButtonShow;
import com.liangtian.mdm.system.button.service.SysButtonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.system.button.service.impl</p>
 * <p>Project Name: chccig-dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-10-13 </p>
 * <p>Description : 用户实现服务接口</p>
 *
 * @author : byr
 * @version :  1.0
 */
@Service
public class SysButtonServiceImpl implements SysButtonService {

    private static final Logger logger = LoggerFactory.getLogger(SysButtonServiceImpl.class);

    //注入dao层接口
    @Autowired
    SysButtonDao sysButtonDao;

    ////@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @Override
    public Map<String, Object> queryButtonList(Map<String, Object> param) {
        Map<String, Object> resMap = new HashMap<>();
        //主数据类型(0:主数据;1:字典表)物料类型再议
        String busType = (String) param.get("busType");
        //0:除了动态模型之外的;1:动态模型
        String isModel = (String) param.get("isModel");
        //业务类型
        String surveyCode = (String) param.get("surveyType");
        //业务类型名称
        String surveyName = (String) param.get("surveyName");
        if ("1".equals(isModel)) {
            param.put("surveyCode", "mdm");
            if ("0".equals(busType))
                param.put("surveyDataType", "mdm");
            else
                param.put("surveyDataType", "dic");
        } else {
            param.put("surveyCode", surveyCode);
            param.put("surveyDataType", "mdm");
        }
        //查询所有的页面按钮
        List<SysButton> buttonLst = sysButtonDao.selectButtons(param);
        //根据用户查询按钮
        List<String> pageList = new ArrayList<String>();
        List<String> buttonList = new ArrayList<String>();
        List<SysPermissionButton> perLst = sysButtonDao.queryButtonPermission(param);
        for (SysPermissionButton sysPermissionButton : perLst) {
            String page = sysPermissionButton.getPage();
            if (null != page && !"".equals(page)) {
                pageList.add(page);
            }
            String pageButton = sysPermissionButton.getPageButton();
            buttonList.add(pageButton);
        }
        Map<String, List<SysButton>> map = new HashMap<String, List<SysButton>>();
        List<SysButton> resList = new ArrayList<SysButton>();
        for (SysButton button : buttonLst) {
            String pageType = button.getPageType();
            String pageName = button.getPageName();
            //String surverType=button.getSurveyType();
            String surveyDataType = button.getSurveyDataType();
            String key = pageType + "@@" + pageName + "@@" + surveyDataType;
            if (map.containsKey(key)) {
                map.get(key).add(button);
            } else {
                List<SysButton> newButLst = new ArrayList<SysButton>();
                newButLst.add(button);
                map.put(key, newButLst);
            }
        }

        for (String key : map.keySet()) {
            SysButtonShow buttonShow = new SysButtonShow();
            String[] keys = key.split("@@");
            buttonShow.setPageType(keys[0]);
            buttonShow.setPageName(keys[1]);
            buttonShow.setSurveyDataType(keys[2]);
            List<SysButton> Lst = map.get(key);//每个页面的按钮
            List<String> buttonTypeLst = new ArrayList<String>();
            for (SysButton sysButton : Lst) {
                buttonTypeLst.add(sysButton.getButtonType());
            }
            buttonShow.setButtonList(Lst);
            buttonShow.setButtonTypes(buttonTypeLst);
            buttonShow.setSurveyName(surveyName);
            resList.add(buttonShow);
        }

        //判断是否全选
        boolean flag = true;
        if (resList.size() > pageList.size()) {
            flag = false;
        }

        resMap.put("isQx", flag);
        resMap.put("buttonShowList", resList);
        resMap.put("pages", pageList);
        resMap.put("pageButtons", buttonList);
        return resMap;
    }

    @Override
    public boolean insertButtons(Map<String, Object> param) {
        boolean flag = false;
        List<SysPermissionButton> butLst = new ArrayList<SysPermissionButton>();
        String surveyType = (String) param.get("surveyType");
        String userId = (String) param.get("userId");
        String uGroupId = (String) param.get("uGroupId");
        String[] pages = (String[]) param.get("pages");
        String[] buttons = (String[]) param.get("buttons");
        if (null != buttons) {
            for (int i = 0; i < buttons.length; i++) {
                SysPermissionButton sysPermissionButton = new SysPermissionButton();
                String[] str = buttons[i].split("@@");
                if (null != pages) {
                    if (!isPage(butLst, str[0])) {
                        for (int j = 0; j < pages.length; j++) {
                            if (str[0].equals(pages[j]))
                                sysPermissionButton.setPage(pages[j]);
                        }
                    }
                }
                sysPermissionButton.setUserId(userId);
                sysPermissionButton.setuGroupId(uGroupId);
                String shiroButtonName = surveyType + ":" + str[1];
                sysPermissionButton.setShiroButton(shiroButtonName);
                sysPermissionButton.setSurveyType(surveyType);
                sysPermissionButton.setPageButton(buttons[i]);
                butLst.add(sysPermissionButton);
            }
        }
        try {
            sysButtonDao.deleteButtonPermission(param);
            if (!butLst.isEmpty()) {
                sysButtonDao.insertButtons(butLst);
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    private boolean isPage(List<SysPermissionButton> butLst, String page) {
        boolean flag = false;
        for (SysPermissionButton sysPermissionButton : butLst) {
            if (page.equals(sysPermissionButton.getPage())) {
                flag = true;
            }
        }
        return flag;
    }
}
